from dataclasses import dataclass
from typing import Dict
from uuid import uuid4

from core.domain.core_domain_entities.package import Package
from core.domain.enums import DocumentClass, PackageStatus

from .document import AccountDocument


@dataclass(eq=False, slots=True)
class AccountPackage(Package):

    REQUIRED_DOC_CLASSES = [DocumentClass.passport, DocumentClass.ZOP]

    def __init__(self, uuid: uuid4, documents: Dict[DocumentClass, AccountDocument]):
        super().__init__(uuid, documents)

    def _check_zop_length(self):
        zop = self.documents.get(DocumentClass.ZOP, '')
        if zop:
            counted_pages = zop.count_pages
            if counted_pages <= 2:
                return PackageStatus.invalid
        else:
            return PackageStatus.invalid

        return PackageStatus.valid

    def return_extracted_entities(self) -> Dict:
        document_texts = {}
        for document_class, document in self.documents.items():
            document_texts[document] = document.return_pages_text()

        return document_texts

    def run_business_logic(self):
        if self._check_zop_length() == PackageStatus.invalid:
            return PackageStatus.invalid

        for expected_document_class, document in self.documents.items():
            if not document.validate_document_class():
                return PackageStatus.invalid

        return PackageStatus.valid
